// import request from "@/utils/requestNode";
import request from "@/utils/request";
import { Resource, ResourceF } from "@/types/resource";
// const ResourceAPI = {
//   /**
//    * 获取资源列表
//    * @returns
//    */
//   getResourceList: () => {
//     return request<any, Resource[]>({
//       url: "/res/resource",
//       method: "get",
//     });
//   },

//   /**
//    *  获取单个资源
//    * @param id 资源id
//    * @returns
//    */
//   getResource: (id: number) => {
//     return request<any, Resource>({
//       url: `/res/resource/${id}`,
//       method: "get",
//     });
//   },

//   /**
//    * 添加资源
//    * @returns
//    */
//   addResource: (body: Resource) => {
//     return request<any, any>({
//       url: "/res/resource",
//       method: "post",
//       data: body,
//     });
//   },

//   /**
//    * 更新资源
//    * @param id 资源id
//    * @param body 资源信息
//    * @returns
//    */
//   updateResource: (body: Resource) => {
//     return request<any, any>({
//       url: `/res/resource/${body.id}`,
//       method: "put",
//       data: body,
//     });
//   },

//   /**
//    *  添加或更新资源
//    * @param body 资源信息
//    * @returns
//    */
//   addOrUpdateResource: (body: Resource) => {
//     if (body.id) {
//       return ResourceAPI.updateResource(body);
//     } else {
//       return ResourceAPI.addResource(body);
//     }
//   },

//   /**
//    * 删除资源
//    * @param id 资源id
//    * @returns
//    */
//   deleteResource: (id: number) => {
//     return request<any, any>({
//       url: `/res/resource/${id}`,
//       method: "delete",
//     });
//   },

//   /**
//    * 下载资源
//    * @param id 资源id
//    * @returns
//    */
//   downloadResource: (id: number) => {
//     return request<any, any>({
//       url: `/res/download/${id}`,
//       method: "get",
//       responseType: "blob",
//     });
//   },
// };
const ResourceAPI = {
  /**
   * 获取资源列表
   * @param sectionId 可选的章节 ID
   * @returns
   */
  getResourceList: (sectionId: number) => {
    let url = `/upload/showResource`;
    if (sectionId !== 0) {
      url += `?sectionId=${sectionId}`;
    }
    return request<any, ResourceF[]>({
      url,
      method: "get",
    });
  },

  /**
   * 添加资源
   * @returns
   */
  addResource: (body: Resource, fd: FormData, signal: AbortSignal) => {
    return request<any, any>({
      url: `/upload/uploadResource?resourceName=${body.name}&resourceType=${body.type}&sectionId=${Number(body.chapter)}`,
      method: "post",
      headers: {
        "Content-Type": "multipart/form-data",
      },
      data: fd,
      signal: signal,
    });
  },

  /**
   * 更新资源
   * @param id 资源id
   * @param body 资源信息
   * @returns
   */
  updateResource: (body: Resource, fd: FormData, signal: AbortSignal) => {
    console.log(body, fd.get("file"));
    console.log(typeof body.chapter);
    return request<any, any>({
      url: `/upload/uploadResource?id=${body.id}&resourceName=${body.name}&resourceType=${body.type}`,
      method: "put",
      headers: {
        "Content-Type": "multipart/form-data",
      },
      data: fd,
      signal: signal,
    });
  },

  /**
   *  添加或更新资源
   * @param body 资源信息
   * @returns
   */
  addOrUpdateResource: (body: Resource, fd: FormData, signal: AbortSignal) => {
    if (body.id) {
      return ResourceAPI.updateResource(body, fd, signal);
    } else {
      return ResourceAPI.addResource(body, fd, signal);
    }
  },

  /**
   * 删除资源
   * @param id 资源id
   * @returns
   */
  deleteResource: (id: number) => {
    return request<any, any>({
      url: `/upload/file?id=${id}`,
      method: "delete",
    });
  },

  /**
   * 下载资源
   * @param id 资源id
   * @returns
   */
  downloadResource: (id: number) => {
    return request<any, any>({
      url: `/upload/download?id=${id}`,
      method: "get",
      responseType: "blob",
    });
  },
};
export default ResourceAPI;
